package windowFactoryPackage;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class ResultsPanel extends JPanel {

	//test with ArrayList, much easier to do query adding results
	static ArrayList<DownloadButton> dbArrayList = new ArrayList<DownloadButton>();
	static ArrayList<ViewButton> vbArrayList = new ArrayList<ViewButton>();
	static ArrayList<String> resultArrayList = new ArrayList<String>();
	static ArrayList<JTextArea> jtaArrayList = new ArrayList<JTextArea>();
	static JPanel [] buttonHolderPanel;
	
	Query q = new Query();
	
	private JPanel buttonPanel, resultPanel, bigPanel;

	public ResultsPanel(int size){
		super();
		bigPanel = new JPanel();
		bigPanel.setLayout(new BorderLayout());
		
		if(size > 6){
			buttonPanel = new JPanel(new GridLayout(size, 1, 20, 20));
			buttonPanel.setBackground(Color.WHITE);
			resultPanel = new JPanel(new GridLayout(size, 1, 20, 20));
			resultPanel.setBackground(Color.WHITE);
			buttonHolderPanel = new JPanel[size];
		} else {
			buttonPanel = new JPanel(new GridLayout(6, 1, 20, 20));
			buttonPanel.setBackground(Color.WHITE);
			resultPanel = new JPanel(new GridLayout(6, 1, 20, 20));
			resultPanel.setBackground(Color.WHITE);
			buttonHolderPanel = new JPanel[6];
		}		
		
		for(int i = 0; i<size; i++){
			buttonHolderPanel[i] = new JPanel(new GridLayout(1,2,20,20));
			buttonHolderPanel[i].setBackground(Color.WHITE);
			
			//disable download button if no file available
			dbArrayList.add(i, new DownloadButton());
			Query.program_id = Query.progId.get(i);
			q.testDownloadAvailable();
			if(Query.testDownloadAvailable.equals("0")){
				dbArrayList.get(i).setEnabled(false);
			}

			vbArrayList.add(i, new ViewButton());

			resultArrayList.add("This is the data from query at position " + i + ": \nAdding more text for testing____________________________________________");

			jtaArrayList.add(i, new JTextArea(resultArrayList.get(i)));
			jtaArrayList.get(i).setEditable(false);

			buttonHolderPanel[i].add(vbArrayList.get(i));
			buttonHolderPanel[i].add(dbArrayList.get(i));
			resultPanel.add(jtaArrayList.get(i));
			buttonPanel.add(buttonHolderPanel[i]);
		}
		bigPanel.add(buttonPanel, BorderLayout.EAST);
		bigPanel.add(resultPanel, BorderLayout.CENTER);
		
	}
	public JPanel getPanel(){
		return bigPanel;
	}
	public void clearLists(){
		dbArrayList.clear();
		vbArrayList.clear();
		resultArrayList.clear();
		jtaArrayList.clear();
	}
}
